<?php

namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    protected $seo_namespace = 'App\Http\Controllers\WebPage\Seo';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        $this->bindRouteMacros();

        parent::boot($router);
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $this->mapWebRoutes($router);

        $this->mapDebugRoutes($router);

        $this->mapApiRoutes($router);

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    protected function mapWebRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require app_path('Http/routes.php');

            require base_path('/routes/web/web.php');

            require base_path('/routes/web/admin.php');

            require base_path('/routes/web/seo.php');
        });

        $router->group([
            'namespace' => $this->seo_namespace,
            'middleware' => 'web',
        ], function ($router) {
            require base_path('/routes/web/seo.php');
        });
    }

    protected function mapDebugRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require base_path('/routes/debug/debug.php');
        });
    }

    protected function mapApiRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'api', 'prefix' => 'api'
        ], function ($router) {
            require app_path('Http/api.php');
            require base_path('routes/api/admin/auth.php');
            require base_path('routes/api/admin/manage.php');
            require base_path('routes/api/admin/service.php');
            require base_path('routes/api/home/home.php');
            require base_path('routes/api/admin/bos.php');
        });
    }


    /**
     * 绑定路由宏
     * @important 宏要在路由 boot 前绑定
     */
    public function bindRouteMacros()
    {
        Router::macro('staticView', function ($uri, $as = null) {
            \Route::get($uri, [
                'as'   => $as ?: $uri,
                'uses' => '\\App\\Http\\Controllers\\WebPageController@view'
            ]);
        });

        /**
         * 跨域预检路由宏
         */
        Router::macro('corsAny', function ($uri = '/') {
            \Route::options($uri . '{any}', '\\App\\Http\\Controllers\\CorsController@preFlightCheck')->where('any', '.*');
        });
    }
}
